package com.jieao.backend.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.jieao.biz.domain.SysAttachment;
import com.jieao.common.core.domain.AjaxResult;
import com.jieao.common.utils.StringUtils;
import com.jieao.common.utils.file.FileConstants;
import com.jieao.common.utils.file.OSSFileUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 通用请求处理
 */
@RestController
public class CommonController {

    @PostMapping("/common/upload")
    public AjaxResult uploadOSSFile(MultipartFile file) {
        if(null == file){
            return AjaxResult.error("参数列表错误");
        }
        try {
            String url = OSSFileUtils.uploadMultipartFile(OSSFileUtils.getOSSClient(), file, "jieao_blog_v1/common/");
            String fileName = url.substring(url.lastIndexOf("/") + 1);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    //editormd上传文件
    @ResponseBody
    @RequestMapping(value = "/common/editormdUpload", method = RequestMethod.POST)
    public JSONObject editormdUpload(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "editormd-image-file", required = false) MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        try {
            //上传
            String url = OSSFileUtils.uploadMultipartFile(OSSFileUtils.getOSSClient(), file, "jieao_blog_v1/editormdUpload/");
            jsonObject.put("success", 1);
            jsonObject.put("message", "上传成功");
            jsonObject.put("url", url);
            return jsonObject;
        } catch (Exception e) {
            jsonObject.put("success", 0);
            jsonObject.put("message", "上传失败");
            return jsonObject;
        }
    }


}
